﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading.Tasks;
using Dapper_Core.BLL.Impl;
using Dapper_Core.BLL.Interface;
using Dapper_Core.Common;
using Dapper_Core.Model;
using Dapper_Core.Table;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace Dapper_Core.Web.Areas.Sys.Controllers
{
    public class Sys_MenuController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult GetExtendMenuList(string search)
        {
            List<Sys_MenuExtend> menus = new Sys_MenuBusiness().GetExtendMenuList(search);
            return Json(menus);
        }

        [HttpPost]
        public IActionResult Add(Sys_Menu menu)
        {
            try
            {
                Sys_MenuBusiness bussiness = new Sys_MenuBusiness();
                if (bussiness.IsExist(menu.MenuCode))
                {
                    return Json(new { Success = false, Message = "本资源已存在，请修改资源编码！" });
                }
                #region 完善Sys_Menu
                menu.MenuId = Guid.NewGuid().ToString();
                menu.OrderNo = menu.MenuCode;
                menu.IsActive = 1;
                menu.IsDeleted = 0;
                menu.Creator = SessionUtility.GetBasicUserInfo().UserName;
                menu.CreateDate = DateTime.Now;
                #endregion
                bool success = bussiness.Add(menu);
                if (success)
                {
                    return Json(new { Success = true, Message = "添加成功！" });
                }
                return Json(new { Success = false, Message = "添加失败，请联系管理员！" });
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(MethodBase.GetCurrentMethod().DeclaringType.FullName, "系统资源添加失败", "menu=" + JsonConvert.SerializeObject(menu), ex, "DapperCore");
                return Json(new { Success = false, Message = "添加失败，请联系管理员！" });
            }
        }

        [HttpPost]
        public IActionResult Edit(Sys_Menu menu)
        {
            try
            {
                Sys_MenuBusiness bussiness = new Sys_MenuBusiness();
                if (bussiness.IsExist(menu.MenuCode, menu.MenuId))
                {
                    return Json(new { Success = false, Message = "本资源已存在，请修改资源编码！" });
                }
                #region 完善Sys_Menu
                Sys_Menu old = bussiness.Get(menu.MenuId);
                old.MenuName = menu.MenuName;
                old.MenuCode = menu.MenuCode;
                old.MenuType = menu.MenuType;
                old.ParentCode = menu.ParentCode;
                old.PageUrl = menu.PageUrl;
                old.Icon = menu.Icon;
                old.OrderNo = menu.MenuCode;
                old.Remarks = menu.Remarks;
                old.Modifier = SessionUtility.GetBasicUserInfo().UserName;
                old.ModifyDate = DateTime.Now;
                #endregion
                bool success = bussiness.UpdateNotNull(old);
                if (success)
                {
                    return Json(new { Success = true, Message = "修改成功！" });
                }
                return Json(new { Success = false, Message = "修改失败，请联系管理员！" });
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "系统资源修改失败", "menu=" + JsonConvert.SerializeObject(menu), ex, "DapperCore");
                return Json(new { Success = false, Message = "修改失败，请联系管理员！" });
            }
        }

        [HttpPost]
        public IActionResult Delete(string deleteIds)
        {
            try
            {
                bool success = new Sys_MenuBusiness().Delete(deleteIds, SessionUtility.GetBasicUserInfo().UserName);
                if (success)
                {
                    return Json(new { Success = true, Message = "删除成功！" });
                }
                return Json(new { Success = false, Message = "删除失败，请联系管理员！" });
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "系统资源软删除失败", "deleteIds=" + deleteIds, ex, "DapperCore");
                return Json(new { Success = false, Message = "删除失败，请联系管理员！" });
            }
        }

        public IActionResult GetMenuPermissionList(string roleCode)
        {
            try
            {
                return Json(new Sys_MenuBusiness().GetMenuPermissionList(roleCode));
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "获取“系统资源 - 角色有权访问的系统资源”之间的对应关系失败", "roleCode=" + roleCode, ex, "DapperCore");
                return Json(new List<Sys_ZTreeMenu>());
            }
        }
    }
}